// pages/my_appointment/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabIdx: 1,
        nowList:{  total: 0,
            current_page: 0,
            last_page: 1,
            data: []
        },
    },
    goappointtime(e){
        console.log(e.currentTarget.dataset.hosptial_id)
        var that = this;
        var json = {};
        json.id=e.currentTarget.dataset.id;
        request.requestLoading(
            "/api/appoint/checkpoint",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
                wx.navigateTo({
                    url: '/pages/select_time/index?hosptial_id='+e.currentTarget.dataset.hosptial_id+'&appoints_id='+e.currentTarget.dataset.id,
                  })
            }else{
                that.init_Data()
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
     
    },
    godetail(e){
     wx.navigateTo({
       url: '/pages/appointment_detail/index?id='+e.currentTarget.dataset.id,
     })
    },
    tabClick(e) {
        this.setData({
            tabIdx: e.currentTarget.dataset.i,
        })
        this.init_Data()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     this.init_Data()
    },
    init_Data(){
        var that = this;
        that.setData({
          'nowList.current_page': 0,
      });
      that.getList();
    
    
      },
      cancelpoint(e){

        var that = this;
        var json = {};
        json.id=e.currentTarget.dataset.id;
        wx.showModal({
          title: '提示',
          content: '是否取消？',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                request.requestLoading(
                    "/api/appoint/cancelpoint",
                    json,
                    'POST',
                  ).then((res) => {
                    if(res.code==1){
                       that.init_Data()
                    }else{
                        wx.showToast({
                          title: res.msg,
                          icon:'none'
                        })
                    }
                   
                   
              
                  }).catch((err) => {
                    console.error(err);
                  });
            }
          }
        })
     
      },
      refresh(e){
        var that = this;
        var index=e.currentTarget.data.index;
        var json = {};
        json.id=e.currentTarget.dataset.id;
        request.requestLoading(
            "/api/appoint/appointDetail",
            json,
            'POST',
          ).then((res) => {
            
            if(res.data){
                var appoints =res.data;
                var data = that.data.nowList.data;
                if(that.data.nowList.data[index].status!=appoints.status){
                    data.splice(index,1)
                   that.setData({
                       'nowList.data':data,
                       'nowList.total':that.data.nowList.total-1,
                   })
                }else{
                  
                    data[index]=appoints;
                    that.setData({
                        'nowList.data':data
                    })
                }
                
            }else{
                wx.showToast({
                  title: '刷新失败',
                  icon:'none'
                })
            }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
      },
      getList()
      {
        var that = this;
        var json = {};
        json.page = Number(that.data.nowList.current_page) + 1;
        json.status =that.data.tabIdx
        if (json.page > 1 && that.data.nowList.current_page >= that.data.nowList.last_page) {       
            return false;
        }
        request.requestLoading(
            "/api/appoint/appointsList",
            json,
            'POST',
          ).then((res) => {
            var nowList = res.data;
            that.setData({
                is_show:true
            })
    
            if (json.page > 1) {
                var now_nowList_data =that.data.nowList.data || [];
                var now_nowList_data = now_nowList_data.concat(nowList.data);
                nowList.data = now_nowList_data;
            }
      
            that.setData({
              nowList: nowList
            });
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      this.init_Data()
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var that =this;

        if (that.data.nowList.current_page >= that.data.nowList.last_page) {
          // functions.toast('已加载到底部了');
          return false;
      } else {
        that.getList();
      }
    },

   
})